package com.xiaolin.oa.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaolin.oa.enums.BuCode;
import com.xiaolin.oa.exception.ServiceException;
import com.xiaolin.oa.mapper.BuOvertimeMapper;
import com.xiaolin.oa.model.dto.BuOvertimeDto;
import com.xiaolin.oa.model.dto.CurrentUser;
import com.xiaolin.oa.model.dto.OvertimeQuery;
import com.xiaolin.oa.model.entity.BuOvertime;
import com.xiaolin.oa.service.BuOvertimeService;
import com.xiaolin.oa.utils.ContextUtil;
import com.xiaolin.oa.utils.SerialNumberGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 加班表 服务实现类
 * </p>
 *
 * @author xiaolin
 * @since 2024-08-22
 */
@Service
public class BuOvertimeServiceImpl extends ServiceImpl<BuOvertimeMapper, BuOvertime> implements BuOvertimeService {
    @Autowired
    private BuOvertimeMapper overtimeMapper;
    @Autowired
    private ContextUtil contextUtil;
    @Autowired
    private SerialNumberGenerator numberGenerator;


    @Override
    public PageInfo<BuOvertimeDto> getOvertimePage(Integer page, Integer pageSize, OvertimeQuery query) {
        PageHelper.startPage(page, pageSize);
        List<BuOvertimeDto> overtimeList = overtimeMapper.getOvertimeList(query);
        return new PageInfo<>(overtimeList);
    }

    /**
     * 校验加班唯一性
     *
     * @param overtime
     * @return
     */
    @Override
    public void checkUnique(BuOvertime overtime) {
        // TODO 校验加班时间是否冲突
        long count = 0;
        if (count > 0) {
            ServiceException.error("加班名称重复了");
        }
    }

    @Override
    public boolean addOvertime(BuOvertime overtime) {
        CurrentUser user = contextUtil.getCurrentUser();
        // 校验加班唯一性
        checkUnique(overtime);
        overtime.setSerialNumber(numberGenerator.generateNumber(BuCode.OVERTIME));
        overtime.setApplicant(user.getId());
        overtime.setCreator(user.getId());
        overtime.setUpdator(user.getId());
        overtimeMapper.insert(overtime);
        return true;
    }

    @Override
    public BuOvertime getOvertime(Integer id) {
        return overtimeMapper.selectById(id);
    }

    @Override
    public boolean updateOvertime(BuOvertime overtime) {
        // 校验加班唯一性
        checkUnique(overtime);
        overtimeMapper.updateById(overtime);
        return true;
    }

    @Override
    public boolean deleteOvertime(Integer id) {
        overtimeMapper.deleteById(id);
        return true;
    }
}
